package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.adapter.FilmsListAdapter;
import com.gykfbxxh.imovie.base.BaseListActivity;
import com.gykfbxxh.imovie.base.bean.DataBean;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.bean.film.FilmListBean;
import com.gykfbxxh.imovie.bean.film.FilmListEntity;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.net.ParamMap;
import com.gykfbxxh.imovie.listener.CollectEditListener;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.utils.HttpUtils;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的收藏
 */
public class MyCollectActivity extends BaseListActivity<FilmListEntity> {
    private CollectEditListener onCollectEditListener;

    public CollectEditListener getOnCollectEditListener() {
        return onCollectEditListener;
    }

    public void setOnCollectEditListener(CollectEditListener onCollectEditListener) {
        this.onCollectEditListener = onCollectEditListener;
    }

    private int mMark;//下拉刷新请求标识
    private TextView tv_selector,tv_del;
    private boolean selFlag = true;
    @Override
    protected void changeView() {

        addBottomView();
        mPageName = "MyCollectActivity" ;
        setTitleShow(true);
        setTitle("我的收藏");
        final TextView textView = setTitleRightText("编辑");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(onCollectEditListener==null) {
//                    return;
//                }
                if(textView.getText().equals("编辑")) {
                    textView.setText("取消");
//                    onCollectEditListener.showAllCheckBox();
                    mAdapter = new FilmsListAdapter(dataList, mActivity, FilmsListAdapter.FILMS_LEFT_IMAGE,true,false);
                    mListView.setAdapter(mAdapter);
                    flCommonBottom.setVisibility(View.VISIBLE);
                }else if(textView.getText().equals("取消")) {
                    textView.setText("编辑");
                    mAdapter = new FilmsListAdapter(dataList, mActivity, FilmsListAdapter.FILMS_LEFT_IMAGE,false,false);
                    mListView.setAdapter(mAdapter);
                    flCommonBottom.setVisibility(View.GONE);
//                    onCollectEditListener.hideAllCheckBox();
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void addBottomView() {
        if (flCommonBottom != null) {
            View view = this.getLayoutInflater().inflate(R.layout.layout_collect_bottom, null);
            tv_selector = (TextView)view.findViewById(R.id.tv_selector);
            tv_del = (TextView)view.findViewById(R.id.tv_del);
            flCommonBottom.addView(view);
            flCommonBottom.setVisibility(View.GONE);
        }
        tv_selector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(selFlag) {
                    mAdapter = new FilmsListAdapter(dataList, mActivity, FilmsListAdapter.FILMS_LEFT_IMAGE,true ,true);
                    mListView.setAdapter(mAdapter);
                    selFlag = false ;
                }else {
                    mAdapter = new FilmsListAdapter(dataList, mActivity, FilmsListAdapter.FILMS_LEFT_IMAGE,true ,false);
                    mListView.setAdapter(mAdapter);
                    selFlag = true ;
                }
            }
        });
        tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAdapter!=null) {
                    HashMap<Integer, FilmEntity> isSelected = ((FilmsListAdapter) mAdapter).getIsSelected();
                    if(isSelected.size()==0) {
                        ToastUtils.makeText("删除选中 0 ");
                        return;
                    }
                    String key = "";
                    String value = "";
                    for(Map.Entry<Integer, FilmEntity> entry : isSelected.entrySet()){
                        key += entry.getKey();
                        value += entry.getValue().getFName();
                    }
                    ToastUtils.makeText("删除选中 key="+key+"value="+value);
                }
            }
        });
    }

    @Override
    protected void changeData(final boolean isRefresh) {
        //获取收藏接口数据
        ParamMap paramMap = new ParamMap();
        paramMap.put("pno",dataPage+"");
        paramMap.put("pagesize","15");
        paramMap.put("allcount","-1");

        HttpUtils.addPostRequest(URIContant.GETMYCOLLECTLOG, new TypeToken<FilmListBean>() {
        }.getType(), mPageName, paramMap, new HttpListener<FilmListBean>() {

            @Override
            public void onStart() {
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(FilmListBean response) {
                if (response != null && response.getResult() != null) {
                    FilmListBean.ResultBean result = response.getResult();
                    if (result != null) {
                        List<FilmListEntity> list = result.getList();
                        if (list != null && list.size() > 0) {
                            if (dataList == null) {
                                dataList = new ArrayList<FilmListEntity>();
                            }
                            if (isRefresh) {
                                dataList.clear();
                            }
                            dataList.addAll(list);
                            if (mAdapter == null) {
                                mAdapter = new FilmsListAdapter(dataList, mActivity, FilmsListAdapter.FILMS_LEFT_IMAGE);
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }
                            if (isRefresh) {
                                if (list.size() > 1) {
                                    String msg = "推荐引擎有<font color='#F9D13F'>" + (list.size() ) + "</font>条新数据";
                                    setLoadView(LOADSUCES, Html.fromHtml(msg));
                                } else {
                                    //updateLoadView(LOADSUCES, "暂无新数据");
                                    setLoadView(LOADSUCES, getString(R.string.pullup_nomore_data));
                                }
                            } else {
                                setLoadView(LOADSUCES, "");
                            }

                        } else {
                            setLoadView(LOADSUCES, getString(R.string.pullup_nomore_data));
                        }
                    } else {
                        setLoadView(DATAERROR, response.getErrorMsg());
                    }
                } else {
                    setLoadView(DATAERROR, response.getErrorMsg());
                }
            }
        });


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        FilmListEntity filmListEntity = dataList.get(position);
        Intent intent = new Intent(this, FilmDetailActivity.class);
        intent.putExtra(PARAMETER1,filmListEntity.getFObject().getFId());
        startActivity(intent);
    }

    private void removeLikeData(String fId, final int position) {
        ParamMap paramMap = new ParamMap();
        paramMap.put("objectid",fId);
        HttpUtils.ParmsMapMD5(paramMap);
        HttpUtils.addPostRequest(URIContant.DELCOLLECTLOGBYOBJECTID,new TypeToken<DataBean>(){}.getType(),  mPageName, paramMap, new HttpListener<DataBean>() {
            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText("删除失败");
            }

            @Override
            public void onSuccess(DataBean response) {
                if(response.noError()) {
                    dataList.remove(position);
                    mAdapter.notifyDataSetChanged();
                    ToastUtils.makeText("删除成功");
                }
            }
        });
    }

}
